package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.dao.JxStudentDao;
import io.renren.modules.sys.entity.JxStudentEntity;
import io.renren.modules.sys.service.JxStudentService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("jxStudentService")
public class JxStudentServiceImpl extends ServiceImpl<JxStudentDao, JxStudentEntity> implements JxStudentService {

    private final  static String DEFIND_PASSWORD="123456";

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<JxStudentEntity> page = this.page(
                new Query<JxStudentEntity>().getPage(params),
                new QueryWrapper<JxStudentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveStudent(JxStudentEntity st){
     st.setCreateTime(new Date());
        String salt = RandomStringUtils.randomAlphanumeric(20);
        if(null==st.getPassword()){
         st.setPassword(DEFIND_PASSWORD);
        }
        st.setSalt(salt);
        st.setPassword(ShiroUtils.sha256(st.getPassword(), st.getSalt()));
    return this.save(st);
    }

    @Override
    public PageUtils getAllWithPage(Map<String, Object> params) {

        PageHelper.startPage(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString()));
        List<JxStudentEntity> list = this.baseMapper.getAllWithPage(params);
        PageInfo<JxStudentEntity> pageInfo = new PageInfo<>(list);
        return new PageUtils(list,(int)pageInfo.getTotal(),pageInfo.getPageSize(),pageInfo.getPageNum());
    }

    @Override
    public int addBatchStudent(List<JxStudentEntity> list) {
        return this.baseMapper.addBatchStudent(list);
    }

    @Override
    public List<JxStudentEntity> getALLlist() {
        return  this.baseMapper.getAllWithPage();
    }


    @Override
    public List<JxStudentEntity> getLastStudents(Map<String, Object> params) {
        return this.baseMapper.getLastStudent(params);
    }
}
